package com.cari.oa.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.cari.oa.domain.CommentEntity;
import com.cari.oa.domain.MarketCostEntity;
import com.cari.oa.domain.SysDictItemEntity;
import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.service.CommentService;
import com.cari.oa.service.MarketCostService;
import com.cari.oa.service.SysDictItemService;
import com.cari.oa.service.SysUserService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.DateUtils;
import com.cari.oa.util.StrUtils;
import com.cari.oa.util.Utils;

/**
 * 费用controller
 * @author --Heyyroup
 * @2013-1-10 上午9:15:39
 */
@Controller
@SessionAttributes({"username","provinces", "usercnname"})
public class CostController {
	
	private static String MODULE_NAME = "project_cost_manage";				//模块名称
	private static int DEFAULT_CHECK_STATUS = 0;  		//初始状态为0
	private static int PASS_CHECK_STATUS = 1;			//审核通过
	private static int REFUSE_CHECK_STATUS = 2;			//审核不通过
	private static int DEFAULT_LINE_NUMBER = 8;			//每页显示的行数
	
	private List<SysDictItemEntity> allProvinceList = null;
	private List<SysUserEntity> allUserList = null;
	
	@Resource
	private SysUserService sysUserService;
	@Resource
	private MarketCostService marketCostService;
	@Resource
	private CommentService commentService;
	@Resource
	private SysDictItemService sysDictService;			//查找省份
	
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
	
	@RequestMapping("/pjcost/cost/list.do")
	public String list(@ModelAttribute("username") String username,
			@ModelAttribute("provinces") Map<String, String> provinces, String applyUser, 
			String selectprovince, Integer sortinfo, Integer curPage, Integer status, ModelMap modelMap){
		//从session里面取出登录用户所关联的省份信息
		List<MarketCostEntity> costList = null;
		if(curPage == null)
			curPage = 1;
		long totalRecords = 0;
		long totalPage = 0;
		if(provinces != null && provinces.size() > 0){
			totalRecords = marketCostService.getCount(applyUser, selectprovince, status, sortinfo, provinces.keySet());
			costList = marketCostService.getPage(applyUser, selectprovince, status, sortinfo, provinces.keySet(), curPage, DEFAULT_LINE_NUMBER);
		}
		totalPage = totalRecords / DEFAULT_LINE_NUMBER + (totalRecords % DEFAULT_LINE_NUMBER != 0 ? 1 : 0);
	
		Map<String, String>  recordsMap = getRecordMap(costList);
		
		Map<String, Date> replyMap = new HashMap<String, Date>();
		if(costList != null){
			for(int i = 0; i < costList.size(); ++i){
				String id = costList.get(i).getId();
				List<CommentEntity> comments = commentService.getListByObjID(id, CommentService.CMT_COST);
				if(comments != null && comments.size() != 0)
					replyMap.put(id, comments.get(0).getCreateTime());
				else
					replyMap.put(id, null);
			}
		}
		
		modelMap.addAttribute("provinces", provinces);
		modelMap.addAttribute("replyMap", replyMap);
		modelMap.addAttribute("applyUser", applyUser);
		modelMap.addAttribute("costList", costList);
		modelMap.addAttribute("selectprovince", selectprovince);
		modelMap.addAttribute("status", status);
		modelMap.addAttribute("sortinfo", sortinfo);
		modelMap.addAttribute("recordsMap", recordsMap);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);

		return "pjcost/cost/list";
	}
	
	@RequestMapping("/pjcost/cost/userlist.do")
	public String userlist(@ModelAttribute("provinces") Map<String, String> provinces, 
			@ModelAttribute("username") String username,
			@ModelAttribute("usercnname") String usercnname,
			String selectprovince, 
			Integer curPage, Integer status, Integer sortinfo, ModelMap modelMap){
		if(curPage == null)
			curPage = 1;
		long totalRecords = 0;
		long totalPage = 0;
		List<MarketCostEntity> costList = null;
		if(provinces != null && provinces.size() > 0){
			totalRecords = marketCostService.getUserCount(username, selectprovince, status, sortinfo, provinces.keySet());
			costList = marketCostService.getUserPage(username, selectprovince, status, sortinfo, provinces.keySet(), curPage, DEFAULT_LINE_NUMBER);
		}
		
		totalPage = totalRecords / DEFAULT_LINE_NUMBER + (totalRecords % DEFAULT_LINE_NUMBER != 0 ? 1 : 0);
		
		Map<String, String>  recordsMap = getRecordMap(costList);
		Map<String, Date> replyMap = new HashMap<String, Date>();
		if(costList != null){
			for(int i = 0; i < costList.size(); ++i){
				String id = costList.get(i).getId();
				List<CommentEntity> comments = commentService.getListByObjID(id, CommentService.CMT_COST);
				if(comments != null && comments.size() != 0)
					replyMap.put(id, comments.get(0).getCreateTime());
				else
					replyMap.put(id, null);
			}
		}
		
		modelMap.addAttribute("provinces", provinces);
		modelMap.addAttribute("replyMap", replyMap);
		modelMap.addAttribute("costList", costList);
		modelMap.addAttribute("selectprovince", selectprovince);
		modelMap.addAttribute("status", status);
		modelMap.addAttribute("recordsMap", recordsMap);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("sortinfo", sortinfo);
        modelMap.addAttribute("totalRecords", totalRecords);
        
		return "pjcost/cost/userlist";
	}
	
	@RequestMapping("/pjcost/cost/statics.do")
	@SuppressWarnings("rawtypes")
	public String list(String province, Integer year, Integer month,
			Integer mode, Integer curPage, ModelMap modelMap){
		if(curPage == null)
			curPage = 1;
		long totalRecords = 0;
		long totalPage = 0;
		double totalexpect = 0;
		double totalactual = 0;
		if(mode == null) mode = 1;
		//从数据库中取得省份信息
		if(allProvinceList == null)
			allProvinceList = sysDictService.getItemsByGroupName("province");
		List<Map> listMap = null;
		if( 1 == mode ){
			listMap = marketCostService.getTotalCost(year, month, curPage, Contants.PAGE_SIZE);
			totalRecords = marketCostService.getTotalCostCount(year, month);
		}
		else{
			listMap = marketCostService.getTotalCostByProvince(province, year);
			totalRecords = listMap.size();
		}
		
		//计算筛选的记录的费用总和
		for(int i = 0; i < listMap.size(); ++i){
			Map map = listMap.get(i);
			Double d1 = (Double) map.get("sum_expect");
			Double d2 = (Double) map.get("sum_actual");
			if( d1 != null )	totalexpect += d1.doubleValue();
			if( d2 != null )	totalactual += d2.doubleValue();
		}
		List<Integer> allYears = marketCostService.getAllRecordsYear();
		totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
		
		
		Map<String, String> provinceMap = new HashMap<String, String>();
		for(SysDictItemEntity entity : allProvinceList){
			provinceMap.put(entity.getValue(), entity.getName());
		}
		
		
		modelMap.addAttribute("totalexpect", totalexpect);
		modelMap.addAttribute("totalactual", totalactual);
		modelMap.addAttribute("selectmode", mode);
		modelMap.addAttribute("allYears", allYears);
		modelMap.addAttribute("provinceMap", provinceMap);
		modelMap.addAttribute("listMap", listMap);
		modelMap.addAttribute("selectyear", year);
		modelMap.addAttribute("selectmonth", month);
		modelMap.addAttribute("curPage", curPage);
	    modelMap.addAttribute("totalPage", totalPage);
	    modelMap.addAttribute("totalRecords", totalRecords);
	    modelMap.addAttribute("selectprovince", province);
	    
	    modelMap.addAttribute("allProvinceList", allProvinceList);
		
		return "pjcost/cost/statistics";
	}
	
	@RequestMapping(value = "/pjcost/cost/userinsert.do", method = RequestMethod.POST)
	@ResponseBody
	public String userinsert(@Valid MarketCostEntity costEntity, @ModelAttribute("username") String username, BindingResult result){
		if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());
		Timestamp time = new Timestamp(System.currentTimeMillis());
		costEntity.setApplyTime(time);
		costEntity.setApplyUser(username);
		costEntity.setMarketstatus(0);
		costEntity.setRegoinstatus(0);
		costEntity.setStatus(0); //status = 0 and checkstatus = '00'表示已提交
		marketCostService.insert(costEntity);
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="/pjcost/cost/userinsert.do", method=RequestMethod.GET)
	public String userinsert(@ModelAttribute("provinces") Map<String, String> provinces, 
			@ModelAttribute("usercnname") String usercnname, ModelMap modelMap, String qs){
		modelMap.addAttribute("provinces", provinces);
		modelMap.addAttribute("qs", qs);
		modelMap.addAttribute("usercnname", usercnname);
		return "pjcost/cost/userinsert";
	}
	
	@RequestMapping(value="/pjcost/cost/usermodify.do", method=RequestMethod.GET)
	public String usermodify(@ModelAttribute("provinces") Map<String, String> provinces, 
			@ModelAttribute("username") String username, String id, String qs, ModelMap modelMap){
		MarketCostEntity costEntity = marketCostService.getById(id.trim());
		if(costEntity == null) return Utils.errorJsonResp("未找到对应的实体对象");
		List<CommentEntity> comments = commentService.getListByObjID(id, CommentService.CMT_COST);
		SysUserEntity recordApplyUser = sysUserService.getByEnname(costEntity.getApplyUser());
		if(comments != null){
			modelMap.addAttribute("comments", comments);
		}
		
		Map<String, String> recordMap = this.getCheckResultMap(costEntity.getId());
		modelMap.addAttribute("recordMap", recordMap);
		modelMap.addAttribute("provinces", provinces);
		modelMap.addAttribute("cost", costEntity);
		modelMap.addAttribute("recordApplyUser", recordApplyUser);
		modelMap.addAttribute("qs", qs);
			
		return "pjcost/cost/usermodify";
	}
	
	@RequestMapping(value="/pjcost/cost/usermodify.do", method=RequestMethod.POST)
	@ResponseBody
	public String usermodify(MarketCostEntity costEntity, @ModelAttribute("username") String username){
		Timestamp time = new Timestamp(System.currentTimeMillis());
		MarketCostEntity entity = marketCostService.getById(costEntity.getId());
		SysUserEntity loginUser = sysUserService.getByEnname(username);
		if(entity.getApplyUser().trim().equals(loginUser.getEnname())){
			if(entity.getStatus() == DEFAULT_CHECK_STATUS 
					&& entity.getMarketstatus() == DEFAULT_CHECK_STATUS
					&& entity.getRegoinstatus() == DEFAULT_CHECK_STATUS){
				entity.setExpectCost(costEntity.getExpectCost());
				entity.setApplyUser(username);
				entity.setRemark(costEntity.getRemark());
				entity.setApplyReason(costEntity.getApplyReason());
				entity.setApplyTime(time);
				marketCostService.update(entity);
			}
			else{
				entity.setRemark(costEntity.getRemark());
				marketCostService.update(entity);
			}
		}
		else{
			return Utils.errorJsonResp("只有记录申请人才能修改记录");
		}
		
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="/pjcost/cost/view.do", method=RequestMethod.GET)
	public String view(@ModelAttribute("provinces") Map<String, String> provinces,
			@ModelAttribute("username") String username,
			String id, ModelMap modelMap, String qs){
		MarketCostEntity costEntity = marketCostService.getById(id.trim());
		List<CommentEntity> comments = commentService.getListByObjID(id, CommentService.CMT_COST);
		if(costEntity == null)
			return Utils.errorJsonResp("未找到对应的实体对象");
		else{
			SysUserEntity recordApplyUser = sysUserService.getByEnname(costEntity.getApplyUser());
			modelMap.addAttribute("provinces", provinces);
			modelMap.addAttribute("cost", costEntity);
			modelMap.addAttribute("qs", qs);
			modelMap.addAttribute("recordApplyUser", recordApplyUser);
			Map<String, String> recordMap = this.getCheckResultMap(costEntity.getId());
			modelMap.addAttribute("recordMap", recordMap);
			
			int marketcheckstatus = costEntity.getMarketstatus();
			int regioncheckstatus = costEntity.getRegoinstatus();
			
			modelMap.addAttribute("marketcheckstatus", marketcheckstatus);
			modelMap.addAttribute("regioncheckstatus", regioncheckstatus);
			
			if(comments != null){
				modelMap.addAttribute("comments", comments);
			}
		}
		
		return "pjcost/cost/view";
	}
	
	@RequestMapping(value="/pjcost/cost/pass.do", method=RequestMethod.GET)
	@ResponseBody
	public String pass(String id, @ModelAttribute("username") String username, 
			@ModelAttribute("usercnname") String usercnname){
		MarketCostEntity costEntity = marketCostService.getById(id);
		if(costEntity == null){
			return Utils.errorJsonResp("未找到对应的实体对象");
		}
		
		if(costEntity.getMarketstatus() == DEFAULT_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == DEFAULT_CHECK_STATUS ){
			//市场部和大区经理均未审批
			costEntity.setStatus(1);
			costEntity.setMarketManager(usercnname);
			costEntity.setMarketstatus(1);
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：市场部经理审核通过，费用记录id为" + id);
		}
		else if(costEntity.getMarketstatus() == DEFAULT_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == PASS_CHECK_STATUS){
			//市场部经理为审批，大区经理审批通过
			costEntity.setCheckStatus("11");	//status = 3 and checkstatus = '11' 表示审核通过==结项中
			costEntity.setStatus(3);
			costEntity.setMarketManager(usercnname);
			costEntity.setMarketstatus(1);
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：市场部经理审核通过，费用记录id为" + id);
		}
		else if(costEntity.getMarketstatus() == PASS_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == PASS_CHECK_STATUS){
			//市场部和大区经理审核通过
			costEntity.setMarketManager(usercnname);
			costEntity.setMarketstatus(1);
			costEntity.setStatus(3);			//status = 3 and checkstatus = '11' 表示审核通过==结项中
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：市场部经理审核通过，费用记录id为" + id);
			return Utils.normalJsonResp();
		}
		else if(costEntity.getMarketstatus() == PASS_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == DEFAULT_CHECK_STATUS ){
			//市场部经理审批通过，大区经理尚未审核
			costEntity.setMarketManager(usercnname);
			costEntity.setMarketstatus(1);
			costEntity.setStatus(1);			//status = 1 and checkstatus = '10'/'01' 表示审核中
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：市场部经理审核通过，费用记录id为" + id);
			return Utils.errorJsonResp("审核已经完成，请等待大区经理审核");
		}
		else if(costEntity.getMarketstatus() == REFUSE_CHECK_STATUS 
				|| costEntity.getRegoinstatus() == REFUSE_CHECK_STATUS){
			return Utils.errorJsonResp("该费用已经被拒绝，如果需要重新开启，请重新填写");
		}
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="/pjcost/cost/regionpass.do", method=RequestMethod.GET)
	@ResponseBody
	public String regionpass(String id, @ModelAttribute("username") String username,
			@ModelAttribute("usercnname") String usercnname){
		MarketCostEntity costEntity = marketCostService.getById(id);
		if(costEntity == null){
			return Utils.errorJsonResp("未找到对应的实体对象");
		}
		if(costEntity.getMarketstatus() == DEFAULT_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == DEFAULT_CHECK_STATUS ){
			//市场部和大区经理均未审批
			costEntity.setStatus(1);
			costEntity.setRegoinManager(usercnname);
			costEntity.setRegoinstatus(1);
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：大区经理审核通过，费用记录id为" + id);
		}
		else if(costEntity.getMarketstatus() == DEFAULT_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == PASS_CHECK_STATUS ){
			//市场部经理未审批，大区经理审批通过
			costEntity.setRegoinManager(usercnname);
			costEntity.setRegoinstatus(1);
			costEntity.setStatus(1);			//status = 1 and checkstatus = '10'/'01' 表示审核中
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：大区经理审核通过，费用记录id为" + id);
		}
		else if(costEntity.getMarketstatus() == PASS_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == PASS_CHECK_STATUS ){
			//市场部和大区经理审核通过
			costEntity.setRegoinManager(usercnname);
			costEntity.setRegoinstatus(1);
			costEntity.setStatus(3);			//status = 3 and checkstatus = '11' 表示审核通过==结项中
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：大区经理审核通过，费用记录id为" + id);
			return Utils.normalJsonResp();
		}
		else if(costEntity.getMarketstatus() == PASS_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == DEFAULT_CHECK_STATUS){
			//市场部经理审批通过，大区经理尚未审核
			costEntity.setRegoinManager(usercnname);
			costEntity.setRegoinstatus(1);
			costEntity.setStatus(3);
			marketCostService.update(costEntity);
			SysOperLogController.addOperLog(username, MODULE_NAME, "费用：大区经理审核通过，费用记录id为" + id);
		}
		else if(costEntity.getMarketstatus() == REFUSE_CHECK_STATUS 
				|| costEntity.getRegoinstatus() == REFUSE_CHECK_STATUS)
			return Utils.errorJsonResp("该费用已经被拒绝，如果需要重新开启，请重新填写");
		
		return Utils.normalJsonResp();
	}
	@RequestMapping(value="/pjcost/cost/refuse.do", method=RequestMethod.GET)
	@ResponseBody
	public String refuse(String id, @ModelAttribute("username") String username, 
			@ModelAttribute("usercnname") String usercnname){
		MarketCostEntity costEntity = marketCostService.getById(id);
		if(costEntity == null){
			return Utils.errorJsonResp("未找到对应的实体对象");
		}
		costEntity.setStatus(2);
		costEntity.setMarketManager(usercnname);
		costEntity.setMarketstatus(2);
		marketCostService.update(costEntity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "费用：市场部经理审核不通过，费用记录id为" + id);
		return Utils.normalJsonResp();
	}
	@RequestMapping(value="/pjcost/cost/regionrefuse.do", method=RequestMethod.GET)
	@ResponseBody
	public String regionrefuse(String id, @ModelAttribute("username") String username, 
			@ModelAttribute("usercnname") String usercnname){
		MarketCostEntity costEntity = marketCostService.getById(id);
		
		if(costEntity == null)	return Utils.errorJsonResp("未找到对应的实体对象");
		
		costEntity.setStatus(2);
		costEntity.setRegoinManager(usercnname);
		costEntity.setRegoinstatus(2);
		marketCostService.update(costEntity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "费用：大区经理审核不通过，费用记录id为" + id);
		return Utils.normalJsonResp();
	}
	@RequestMapping(value="/pjcost/cost/delete.do", method=RequestMethod.GET)
	@ResponseBody
	public String delete(String id, @ModelAttribute("username") String username){
		MarketCostEntity costEntity = marketCostService.getById(id);
		
		if(costEntity == null)	return Utils.errorJsonResp("未找到对应的实体对象");
		
		if(!costEntity.getApplyUser().trim().equals(username))	return Utils.errorJsonResp("只有申请人才能删除该记录");
		
		if(costEntity.getMarketstatus() == DEFAULT_CHECK_STATUS 
				&& costEntity.getRegoinstatus() == DEFAULT_CHECK_STATUS)
			marketCostService.delete(costEntity);
		else
			return Utils.errorJsonResp("该记录已经被审核或者审核进行中，不能删除");

		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="/pjcost/cost/modifyactual.do", method=RequestMethod.POST)
	@ResponseBody
	public String modifyActual(String id, String actualCost, String actualDesc, 
			String wipeoutUser, String remark,
			@ModelAttribute("username") String username){
		MarketCostEntity costEntity = marketCostService.getById(id);
		if(costEntity == null)
			return Utils.errorJsonResp("未找到对应的实体对象");
		
		if(!costEntity.getApplyUser().trim().equals(username))
			return Utils.errorJsonResp("只有申请人才能进行结项操作");
		
		if(StrUtils.isEmpty(actualCost))
			return Utils.errorJsonResp("结项前请填写实际金额,实际细项描述部分内容以及报销人员信息");
		
		if(costEntity.getMarketstatus() != PASS_CHECK_STATUS
				|| costEntity.getRegoinstatus() != PASS_CHECK_STATUS)
			return Utils.errorJsonResp("该记录审核未通过，不能结项");
		
		costEntity.setActualCost(Double.parseDouble(actualCost));

		costEntity.setActualDesc(actualDesc);
		costEntity.setWipeoutUser(wipeoutUser);
		costEntity.setRemark(remark);
		//status = 4表示记录已经结项成功
		costEntity.setStatus(4);	
		marketCostService.update(costEntity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "费用记录结项，记录id为" + id);
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="/pjcost/cost/closerecord.do", method=RequestMethod.GET)
	@ResponseBody
	public String closeRecord(String id, @ModelAttribute("username") String username){
		MarketCostEntity costEntity = marketCostService.getById(id);
		if(costEntity == null){
			return Utils.errorJsonResp("未找到对应的实体对象");
		}
		if(costEntity.getStatus() != 4)
			return Utils.errorJsonResp("该记录尚未结项，不能归档");
		
		costEntity.setStatus(5);
		marketCostService.update(costEntity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "费用记录归档，记录id为" + id);
		
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="pjcost/cost/reply.do", method=RequestMethod.POST)
	@ResponseBody
	public String reply(CommentEntity newreply, String costId,
			@ModelAttribute("username") String username,
			@ModelAttribute("usercnname") String usercnname){
		newreply.setUserEnname(username);
		newreply.setUserCnname(usercnname);
		newreply.setObjectType(CommentService.CMT_COST);
		newreply.setObjectId(costId);
		commentService.insert(newreply);
		
    	
    	HashMap<String, String> resultMap = new HashMap<String, String>();
    	resultMap.put("username", usercnname);
    	resultMap.put("createtime", DateUtils.date2Str(newreply.getCreateTime(), DateUtils.NORMAL_FORMAT));
    	resultMap.put("content", newreply.getContent());
		return JSON.toJSONString(resultMap);
	}

	/**
	 * 将用户的中文名称和英文名称对应
	 * @author -- Heyyroup
	 * @2013-1-24 下午5:47:04
	 * @param costList
	 * @return
	 */
	private Map<String, String> getRecordMap(List<MarketCostEntity> costList){
		if( allUserList == null ) allUserList = sysUserService.getAll();
		Map<String, String> map = new HashMap<String, String>();
		if(costList == null)
			return null;

		for(int i = 0; i < costList.size(); ++i){
			MarketCostEntity entity = costList.get(i);
			for(SysUserEntity sysUserEntity : allUserList){
				if(sysUserEntity.getEnname().equals(entity.getApplyUser())){
					map.put(entity.getApplyUser(), sysUserEntity.getCnname());
				}
			}
		}
		
		return map;
	}
	
	/**
	 * 生成审核结果
	 * @author -- Heyyroup
	 * @2013-1-30 下午3:35:33
	 * @param entity
	 * @return
	 */
	private Map<String, String> getCheckResultMap(String entityID){
		Map<String, String> map = new HashMap<String, String>();
		StringBuilder checkResult = new StringBuilder();
		
		MarketCostEntity entity = marketCostService.getById(entityID);
		
		if(entity != null){
			if(entity.getMarketstatus() == DEFAULT_CHECK_STATUS){
				checkResult.append( "市场部经理[");
				checkResult.append(entity.getMarketManager());
				checkResult.append("]:未审核");
				checkResult.append("\n");
			}
			if(entity.getMarketstatus() == PASS_CHECK_STATUS){
				checkResult.append( "市场部经理[");
				checkResult.append(entity.getMarketManager());
				checkResult.append("]:审核通过");
				checkResult.append("\n");
			}
			if(entity.getMarketstatus() == REFUSE_CHECK_STATUS){
				checkResult.append( "市场部经理[");
				checkResult.append(entity.getMarketManager());
				checkResult.append("]:审核不通过");
				checkResult.append("\n");
			}
			
			if(entity.getRegoinstatus() == DEFAULT_CHECK_STATUS){
				checkResult.append( "大区经理[");
				checkResult.append(entity.getRegoinManager());
				checkResult.append("]:未审核");
				checkResult.append("\n");
			}
			if(entity.getRegoinstatus() == PASS_CHECK_STATUS){
				checkResult.append( "大区经理[");
				checkResult.append(entity.getRegoinManager());
				checkResult.append("]:审核通过");
				checkResult.append("\n");
			}
			
			if(entity.getRegoinstatus() == REFUSE_CHECK_STATUS){
				checkResult.append( "大区经理[");
				checkResult.append(entity.getRegoinManager());
				checkResult.append("]:审核不通过");
				checkResult.append("\n");
			}
				
			map.put(entityID, checkResult.toString());
		}
		else
			map.put(entityID, "对象未找到");
		
		
		
		return map;
	}
	
}
